﻿@model object
@{ 
    ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix.Replace(ViewData.ModelMetadata.PropertyName, "").Trim('.');
    var propertyName = ViewData["name"] == null ? ViewData.ModelMetadata.PropertyName : ViewData["name"].ToString(); 
}
<tr>
    <th>
        <label for="@propertyName">
            @ViewData.ModelMetadata.GetDisplayName().Localize()</label>

        @if (!string.IsNullOrEmpty(ViewData.ModelMetadata.Description))
        {
            <a  class="tooltip-link" title="@ViewData.ModelMetadata.Description "></a>
        }
    </th>
    <td>
        <input type="hidden" name="@propertyName" value="@Model" id="hidden-@(propertyName)"/>
        @Html.DropDownList(propertyName, ViewData.ModelMetadata.GetDataSource()
                    .GetSelectListItems(ViewContext.RequestContext, "").SetActiveItem(Model),
                    Html.GetUnobtrusiveValidationAttributes(propertyName, ViewData.ModelMetadata).Merge("class", ViewData["class"]).Merge("disabled", "disabled"))
        <a class="action" id="layout-edit">@Html.IconImage("edit small")</a>
        <a href="@Html.Raw(Url.Action("ChangeLayout", ViewContext.RequestContext.AllRouteValues())) " class="action" id="layout-save">@Html.IconImage("save small")</a>
        <a class="action" id="layout-cancel">@Html.IconImage("minus small")</a>
        @Html.ValidationMessage(ViewData.ModelMetadata, new { name = ViewData["name"] })
    </td>
</tr>
<script type="text/javascript">

    $(function () {
        var action = '@ViewContext.RequestContext.GetRequestValue("action").ToLower() ';
        $('#hidden-@propertyName').removeAttr('disabled');
        var layoutEdit = $('#layout-edit').click(function () {
            layoutEdit.hide();
            $('#Layout').removeAttr('disabled');
            $('#layout-save,#layout-cancel').show();
            $('#hidden-@propertyName').attr('disabled', 'disabled');
        });
        function disableEdit() {
            $('#Layout').attr('disabled', 'disabled')
            $('#hidden-@propertyName').removeAttr('disabled');
            $('#layout-save,#layout-cancel').hide();
            $('#layout-edit').show();;
        }
        var layoutSave = $('#layout-save').click(function () {
            var handle = $(this);
            if (confirm('@("Are you sure you want to change the layout?".Localize())')) {

                $.post(handle.attr('href'),
                { layout: $('#Layout').val() },
                function (response) {
                    parse_JsonResultData(response);
                    disableEdit();
                });

                window.loading.show();
            };

            return false;
        }).hide();
        $('#layout-cancel').click(function () {
            $('#Layout').val($('#hidden-@propertyName ').val());
            disableEdit();
        }).hide();

        $('#Layout').attr('disabled', 'disabled');
        if (action == 'create') {
            layoutEdit.hide();
        }
    });
</script>
